package com.dc.datacollect.components.thread_pool;

import com.dc.datacollect.constant.type.ThreadPoolType;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.*;

//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
@EnableAsync
public class MyThreadConfig {

    @Bean(name= ThreadPoolType.BASE_THREAD_POOL)
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
       return new ThreadPoolExecutor(pool.getCoreSize(),
                pool.getMaxSize(),pool.getKeepAliveTime(),
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

    }

    @Bean(name= ThreadPoolType.SCHEDULED_THREAD_POOL)
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ScheduledThreadPoolExecutor(pool.getCoreSize(),
                Executors.defaultThreadFactory());
    }


}
